package com.example.springlearn.controller;

import com.example.springlearn.bean.AnsInfo;
import com.example.springlearn.bean.QuestionInfo;
import com.example.springlearn.oj.OjRun;
import com.example.springlearn.service.AnsService;
import com.example.springlearn.service.QuestionService;
import com.example.springlearn.util.AnswerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    AnsService ansService;

    @Autowired
    QuestionService questionService;

    @Autowired
    OjRun run;

    @GetMapping(value = "/list")
    public String questionList(Model model, @CookieValue("uid") String uid) {
        List<AnsInfo> ansInfos = ansService.queryGradesByUserId(Integer.parseInt(uid));
        List<QuestionInfo> questionInfos = questionService.queryAll();
        Map<QuestionInfo, Boolean> answerState = AnswerUtil.answerState(questionInfos, ansInfos);
        model.addAttribute("queState", ansInfos);
        model.addAttribute("allQue", questionInfos);
        model.addAttribute("answerState", answerState);
        return "/exam/list";
    }

    @GetMapping(value = "/question/{qid}")
    public String question(@PathVariable Integer qid, Map<String, Object> map) {
        map.put("queInfo", questionService.queryByQid(qid));
        return "/exam/question";
    }

    @ResponseBody
    @GetMapping(value = "/q/{qid}")
    public QuestionInfo getQue(@PathVariable("qid") Integer qid) {
        return questionService.queryByQid(qid);
    }

    @ResponseBody
    @PostMapping(value = "/question/{qid}")
    public List<Map<Boolean, String>> question(@PathVariable Integer qid,
                           @CookieValue("uid") Integer uid,
                           @CookieValue("username") String username,
                           @RequestParam("codeType") String codeType,
                           @RequestParam("code") String code) throws IOException, InterruptedException {
        QuestionInfo question = questionService.queryByQid(qid);

        List<Map<Boolean, String>> judge = run.judge(String.format("%s_%d", username, uid), code, codeType, question);
//        ansService.updateGrade();
        return judge;
    }

    @GetMapping(value = "/add")
    public String addQuestion() {
        return "/exam/update";
    }

    @PostMapping(value = "/add")
    public String addQuestion(QuestionInfo questionInfo) {
        log.info("insert question | {}", questionInfo);
        questionService.insertQue(questionInfo);
        return "redirect:/exam/list";
    }

    @GetMapping(value = "/update")
    public String updateQuestion(@PathParam("qid") Integer qid, Map<String, Object> map) {
        QuestionInfo questionInfo = questionService.queryByQid(qid);
        map.put("question", questionInfo);
        return "/exam/update";
    }

    @PostMapping(value = "/update")
    public String updateQuestion(QuestionInfo questionInfo) {
        log.info("update question {}", questionInfo);
        questionService.updateQue(questionInfo);
        return "redirect:/exam/list";
    }

    @GetMapping(value = "/del/{qid}")
    public String deleteQuestion(@PathVariable Integer qid) {
        log.info("{}", qid);
//        questionService.deleteQue(qid);
        return "redirect:/exam/list";
    }

}
